/*
 * allocate_region.cpp
 *
 * Copyright 2013 Stefan Fruhner
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 *
 */
#include <mylibs/myexception.hpp>
#include <mylibs/CardiacMesh.hpp>
#include <mylibs/myinifiles.hpp>
#include <mylibs/cmdline.hpp>
#include <mylibs/myio.hpp>
#include <mylibs/Profiler.hpp>
#include <iostream>
#include <fstream>


using namespace std;
using namespace mylibs;

bool verbose = false;

int main(int argc, char **argv){
	myIniFiles ini(argc, argv);

	ini.set_info("Allocate a region ID to a set of vertices.");
	ini.register_param("mesh", "i", "Basename of the mesh to load.");
	ini.register_param("vtxfile", "l", "Filename of the vtx file containing the vertices.");
	ini.register_param("region", "r", "Region ID to apply.");
	ini.register_param("update", "u", "Region ID to by updated. Other regions remain untouched. Default : -1 = all regions ");

	mystring meshfile;
	mystring vtxfile = mystring();

	meshfile = ini.read("mesh", mystring());
	vtxfile  = ini.read("vtxfile", mystring());

	int region = ini.read("region", 0);
	int update = ini.read("update", -1);

	if (meshfile.empty()) 	ini.print_help("mesh");


	CardiacMesh mesh(meshfile, true); 	// load the mesh
	mesh.read_vtx(vtxfile.c_str());
	Profiler prof(mesh.vtx.size());
	for (list<int>::iterator it = mesh.vtx.begin(); it != mesh.vtx.end(); ++it){
		mesh.set_region_at_vtxID(*it, region, update);
		prof.info();
	}
	prof.finalize();

	mystring src(mesh.GetBasename().file_new_ext(".elem"));
	mystring tgt(mesh.GetBasename().file_new_ext(".elem.orig"));
	IO::cp(src, tgt);
	mesh.save_elem(src.c_str());

	return 0;
}
